﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;

namespace MyVoteMVC.Service
{
    public class VoteProjectService : IProjectService
    { 
        private const string EmailAddress = "@gzdi.com";//接收方邮箱    
        IUnitOfWork unitOfWork;
        IRepository<VT_ProjectImage> projectImageRepository;
        IRepository<VT_Projects> projectsRepository;
        IRepository<VT_Projects> adminProjectsRepository;
        IRepository<VT_Questions> questionsRepository;
        IRepository<VT_Items> itemsRepository;
         
        public VoteProjectService()
        {
            unitOfWork = Factory.GetInstance<IUnitOfWork>();
            projectsRepository = unitOfWork.GetRepository<VT_Projects>();
            projectImageRepository = unitOfWork.GetRepository<VT_ProjectImage>();
            adminProjectsRepository = unitOfWork.GetAdminRepository<VT_Projects>();
            questionsRepository = unitOfWork.GetRepository<VT_Questions>();
            itemsRepository = unitOfWork.GetRepository<VT_Items>();
        }

        //增加
        public bool AddProjectImage(string projectID, byte[] projectImage, byte[] ThumbnailImage)
        {
            VT_ProjectImage v_ProjectImage = new VT_ProjectImage();
            unitOfWork.BeginTransaction();
            v_ProjectImage.Project_ID = projectID;
            v_ProjectImage.Project_Image = projectImage;
            v_ProjectImage.Project_Thumbnail = ThumbnailImage;
            projectImageRepository.Add(v_ProjectImage);
            unitOfWork.CommitTransaction();
            return true;
        }
        //删除
        public bool DeleteProjectImage(string projectID)
        {
            VT_ProjectImage v_ProjectImage = projectImageRepository.GetMany(o => o.Project_ID == projectID).FirstOrDefault();
            if (v_ProjectImage != null)
            {
                unitOfWork.BeginTransaction();
                projectImageRepository.Delete(v_ProjectImage);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        //修改
        public bool ModifyProjectImage(string projectID, byte[] projectImage, byte[] ThumbnailImage)
        {
            VT_ProjectImage v_ProjectImage = projectImageRepository.GetMany(o => o.Project_ID == projectID).FirstOrDefault();
            if (v_ProjectImage != null)
            {
                unitOfWork.BeginTransaction();
                v_ProjectImage.Project_Image = projectImage;
                v_ProjectImage.Project_Thumbnail = ThumbnailImage;
                projectImageRepository.Update(v_ProjectImage);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        //增加
        public string CreateProjects(string projectYear, string projectAddress, string projectName, string projectType, string projectAwards, string projectDescription, byte[] projectImage, byte[] ThumbnailImage)
        {
            VT_Projects v_Projects = new VT_Projects();
            string strID = Guid.NewGuid().ToString();
            unitOfWork.BeginTransaction();
            v_Projects.Project_Year = projectYear;
            v_Projects.Project_Address = projectAddress;
            v_Projects.Project_Name = projectName;
            v_Projects.Project_Type = projectType;
            v_Projects.Project_ID = strID;
            v_Projects.Project_Awards = projectAwards;
            v_Projects.Project_Description = projectDescription;
            v_Projects.ProjectImage = new VT_ProjectImage();
            v_Projects.ProjectImage.Project_ID = strID;
            v_Projects.ProjectImage.Project_Image = projectImage;
            v_Projects.ProjectImage.Project_Thumbnail = ThumbnailImage;
            adminProjectsRepository.Add(v_Projects);
            unitOfWork.CommitTransaction();
            return strID;
 
        }

        //编辑
        public bool EditProjects(string projectID, string projectYear, string projectAddress, string projectName, string projectType, string projectAwards, string projectDescription)
        {
            VT_Projects v_Projects = projectsRepository.GetMany(o => o.Project_ID == projectID).FirstOrDefault();
            if (v_Projects != null)
            {
                unitOfWork.BeginTransaction();
                v_Projects.Project_Year = projectYear;
                v_Projects.Project_Address = projectAddress;
                v_Projects.Project_Name = projectName;
                v_Projects.Project_Type = projectType;
                v_Projects.Project_Awards = projectAwards;
                v_Projects.Project_Description = projectDescription;
                adminProjectsRepository.Update(v_Projects);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        //删除
        public bool DeleteProjects(string projectID)
        {
            VT_Projects v_Projects = projectsRepository.GetMany(o => o.Project_ID == projectID).FirstOrDefault();
            if (v_Projects != null)
            {
                unitOfWork.BeginTransaction();
                adminProjectsRepository.Delete(v_Projects);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        //删除
        public bool DeleteProjects(List<VT_Projects> v_Projects)
        {
            unitOfWork.BeginTransaction();
            adminProjectsRepository.Delete(v_Projects);     
            unitOfWork.CommitTransaction();
            return true;
        }

        //获取 所有
        public IList<VT_Projects> GetAllProjects()
        {
            IList<VT_Projects> v_Projects = projectsRepository.GetAll().OrderBy(o=>o.Project_Year).OrderBy(o=>o.Project_Type).ToList(); 
            return v_Projects;
        }
        //获取 年份
        public IList<VT_Projects> GetProjectsByYear(string strYear)
        {
            IList<VT_Projects> v_Projects = projectsRepository.GetMany(o => o.Project_Year == strYear).ToList();
            return v_Projects;
        }
        //获取 类型
        public IList<VT_Projects> GetProjectsByType(string strType)
        {
            IList<VT_Projects> v_Projects = projectsRepository.GetMany(o => o.Project_Type == strType).ToList();
            return v_Projects;
        }

        //获取 所有出现的年份
        public List<string> GetALLYear()
        {
            List<string> msx = (from v_projects in projectsRepository.QueryableEntity()
                                where v_projects.Project_Year != null && v_projects.Project_Year!=string.Empty
                                orderby v_projects.Project_Year
                       select v_projects.Project_Year).Distinct().ToList();
            return msx;
        }
        //获取所有出现的类型
        public IList<string> GetALLType()
        {
            List<string> msx = (from projects in projectsRepository.QueryableEntity()
                                where projects.Project_Type != null && projects.Project_Type != string.Empty
                       select projects.Project_Type).Distinct().ToList();
            return msx;
        }

        public IList<VT_Projects> FuzzySearch(string strFindContent)
        {
            var v_Projects = from vProjects in projectsRepository.QueryableEntity()
                             where vProjects.Project_Address.Contains(strFindContent) || vProjects.Project_Awards.Contains(strFindContent) || vProjects.Project_Description.Contains(strFindContent)
                             || vProjects.Project_Name.Contains(strFindContent) || vProjects.Project_Type.Contains(strFindContent) || vProjects.Project_Year.Contains(strFindContent)
                             select vProjects;
            IList<VT_Projects> v_ProjectsList = v_Projects.ToList();
            return v_ProjectsList;
        }
        //活动里面的问题关联到的所有项目的所有年跟类型
        public List<string> GetProjectsYearByActivity(string activityID)
        {
            List<string> msx = (from v_Projects in projectsRepository.QueryableEntity()
                                     join v_items in itemsRepository.QueryableEntity() on v_Projects.Project_ID equals v_items.Project_ID
                                     join v_questions in questionsRepository.QueryableEntity() on v_items.Question_ID equals v_questions.Question_ID
                                     where v_Projects.Project_Year != null && v_Projects.Project_Year != string.Empty && v_questions.Activity_ID == activityID
                                     orderby v_Projects.Project_Year
                                     select v_Projects.Project_Year).Distinct().ToList();
            return msx;
        }

        public List<string> GetProjectsYearGroupByActivity(string activityID)
        {
            List<string> msx = (from v_Projects in projectsRepository.QueryableEntity()
                                join v_items in itemsRepository.QueryableEntity() on v_Projects.Project_ID equals v_items.Project_ID
                                join v_questions in questionsRepository.QueryableEntity() on v_items.Question_ID equals v_questions.Question_ID
                                where v_Projects.Project_Year != null && v_Projects.Project_Year != string.Empty && v_questions.Activity_ID == activityID
                                orderby v_Projects.Project_Year
                                select v_Projects.Project_Year).Distinct().ToList();

            List<string> result = new List<string>();
            foreach (string year in msx)
            {
                if (!string.IsNullOrEmpty(year))
                {
                    string group = year.Substring(0, 3) + "0";
                    if (!result.Contains(group))
                    {
                        result.Add(group);
                    }
                }
            }

            return result;
        }

        public List<string> GetProjectsTypeByActivity(string activityID)
        {
            List<string> msx = (from v_Projects in projectsRepository.QueryableEntity()
                                join v_items in itemsRepository.QueryableEntity() on v_Projects.Project_ID equals v_items.Project_ID
                                join v_questions in questionsRepository.QueryableEntity() on v_items.Question_ID equals v_questions.Question_ID
                                where v_Projects.Project_Type != null && v_Projects.Project_Type != string.Empty && v_questions.Activity_ID == activityID
                                orderby v_Projects.Project_Type
                                select v_Projects.Project_Type).Distinct().ToList();
            return msx;
        }

        public List<VT_Projects> GetALLProjectsByActivity(string activityID)
        {
            List<VT_Projects> msx = (from v_Projects in projectsRepository.QueryableEntity()
                                join v_items in itemsRepository.QueryableEntity() on v_Projects.Project_ID equals v_items.Project_ID
                                join v_questions in questionsRepository.QueryableEntity() on v_items.Question_ID equals v_questions.Question_ID
                                where v_questions.Activity_ID == activityID
                                select v_Projects).ToList();
            return msx;
        }

        public List<VT_Projects> FindAllProject(string strYear,string strType,string key)
        {
            List<VT_Projects> msx = null;
            if (!string.IsNullOrEmpty(strYear) && !string.IsNullOrEmpty(strType) && !string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetMany(o => o.Project_Year == strYear && o.Project_Type == strType && o.Project_Name.Contains(key)).ToList();
            }
            else if (!string.IsNullOrEmpty(strYear) && !string.IsNullOrEmpty(strType) && string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetMany(o => o.Project_Year == strYear && o.Project_Type == strType).ToList();
            }
            else if (!string.IsNullOrEmpty(strYear) && string.IsNullOrEmpty(strType) && !string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetMany(o => o.Project_Year == strYear && o.Project_Name.Contains(key)).ToList();
            }
            else if (string.IsNullOrEmpty(strYear) && !string.IsNullOrEmpty(strType) && !string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetMany(o => o.Project_Type == strType && o.Project_Name.Contains(key)).ToList();
            }
            else if (!string.IsNullOrEmpty(strYear) && string.IsNullOrEmpty(strType) && string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetMany(o => o.Project_Year == strYear).ToList();
            }
            else if (string.IsNullOrEmpty(strYear) && !string.IsNullOrEmpty(strType) && string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetMany(o => o.Project_Type == strType).ToList();
            }
            else if (string.IsNullOrEmpty(strYear) && string.IsNullOrEmpty(strType) && !string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetMany(o => o.Project_Name.Contains(key)).ToList();
            }
            else if (string.IsNullOrEmpty(strYear) && string.IsNullOrEmpty(strType) && string.IsNullOrEmpty(key))
            {
                msx = projectsRepository.GetAll().ToList();
            }
            return msx;
        }

        #region IDisposable Members
        private bool m_disposed;
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    unitOfWork.Dispose();
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~VoteProjectService()
        {
            Dispose(false);
        }
        #endregion

        #region IProjectService Members


        public VT_Projects GetProjectById(string projectId)
        {
            return projectsRepository.GetEntityById(projectId);
        }

        #endregion
    }

}
